%Loads each data set in the file problems{i},
%executes the three algorithms for each value of gamma in gammas.
%It evaluates the error in the coefficients by evaluating ||B_estimated - B_original||_F. The errors are stored 
%in the vectors Emlm, Ehyb, Ereg respectively for the maximum likelihood, hybrid estimation and regularized iterative regression.
%For some of the values of gamma the algorithms might stagnate, and reach the maximum m=number of iterations, the flags
%stored in the vectors Creg, Chyb correspond to the values of gamma for which iterative regression and hybryd mle regression converged.
%The vectors are then stored in the file defined by result_Files{i};
tol = 1e-4;
cvx_solver sdpt3;

problem_files = {'../Data/cycle_graph.mat','../Data/random_graph.mat'}
result_files  = {'../Data/res_cycle.mat','../Data/res_random.mat'}

%Range of gammas to test
gammas = [0.05:0.05:1];
gamma = 0.1;
cvx_quiet(1)

for q = 1:length(problem_files)
	load(problem_files{q});
	Emlm = [];
	Ereg = [];
	Erwr = [];
	Crwr = [];
	Creg = [];
	Chyb = [];

	for j = 1:length(gammas)
		
		fprintf('MLM\n')
		cvx_quiet(0)
		Bmlm = regular_mlm(C, n, p, gammas(j)); Ymlm = compute_D(Bmlm'*Bmlm, n, p);
		cvx_quiet(1)
		[Breg,creg] = regular_rgr(C, n, p, gammas(j),tol,10); Yreg = compute_D(Breg'*Breg, n, p);
		[Brwr,crwr] = regular_rgr_wreg(C, n, p, gammas(j),tol,10); Yreg = compute_D(Breg'*Breg, n, p);
		
		
		Emlm = [Emlm norm(B-Bmlm,'fro')];
		Erwr = [Erwr norm(B-Brwr,'fro')];
		Ereg = [Ereg norm(B-Breg,'fro')];

		Creg = [Creg,creg];
		Crwr = [Crwr,crwr]; 
		if(creg == false) fprintf('Regression did not converge \n'); end
		if(crwr == false) fprintf('Hybrid method did not converge \n'); end
		fprintf('Iteration %i, gamma %d, ML error %d, Hybryd method error %d, regression error %d \n',j,gammas(j),Emlm(end),Ereg(end),Ereg(end));

	end

	%close all
	%figure 
	%hold on
	%plot([Emlm;Ehyb.*Chyb,Ereg.*Creg]');
	%legend('Max Likelihood','Hybrid','Regression');
	%hold off


save result_files{q} Emlm Erwr Ereg Creg Crwr gammas N,n,p

end
